package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.*;
import com.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/Repairquotation")
public class RepairquotationController {
    @Resource
    private RepairQuotationService repairQuotationService;
    @Resource
    private OwnersService ownerService;
    @Resource
    private HotGasService hotGasService;
    @Resource
    private StopcarmoneyService stopCarMoneyService;
    @Resource
    private ItemService itemService;
    @Resource
    private HydropowerService hydropowerService;
    @Resource
    private PayService payService;
    /*---------------------报修缴费--------------------------*/
    @ResponseBody
    @RequestMapping("/addQuotation")
    public Result addQuotation(RepairquotationVo repairquotationVo){
        String ownername=repairquotationVo.getRepairpeople();
        Owners owners = ownerService.selectOwnersByName(ownername);
        repairquotationVo.setOwnerid(owners.getId());
        repairquotationVo.setRepairstype(repairquotationVo.getRepairtype());
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        repairquotationVo.setCreatetime(formatter.format(date));
        repairquotationVo.setStateid(1);
        System.out.println(repairquotationVo);
        try {
            int i = repairQuotationService.addQuotation(repairquotationVo);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("添加失败");
        }

    }
    @RequestMapping("/towillPay")
    public String towillPay(){
        return "/pay/willPay";
    }
    @RequestMapping("/ownerSelectQuotation")
    @ResponseBody
    public Result ownerSelectQuotation(RepairquotationVo repairquotationVo,Integer ownerid){
        PageInfo<Repairquotation> repairquotationPageInfo = repairQuotationService.OwnerSelect(repairquotationVo,ownerid);
        System.out.println(repairquotationPageInfo);
        return Result.success(repairquotationPageInfo);
    }
    @RequestMapping("/OwnerUpdateQuotationState")
    @ResponseBody
    public Result OwnerUpdateQuotationState(Integer id){
        try {
            int i = repairQuotationService.OwnerUpdateQuotationState(id);
            return Result.success("缴费成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("缴费失败");
        }

    }
    /*------------------------------停车费----------------------------------------*/
    @ResponseBody
    @RequestMapping("/OwnerSelectStopCarByOwnerphone")
    public Result OwnerSelectStopCarMoneyByOwnerId(String ownerphone){
        List<Stopcarmoney> stopcarmonies = stopCarMoneyService.OwnerSelectStopCarByOwnerphone(ownerphone);
        return Result.success(stopcarmonies);
    }
    @ResponseBody
    @RequestMapping("/UpdateIsNoById")
    public Result updateIsNoById(Integer id){
        try {
            int i = stopCarMoneyService.updateIsNoById(id);
            return Result.success("完成缴费");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("缴费失败");
        }

    }
    /*---------------------------暖气缴费------------------------------------*/
    @RequestMapping("/OwnerSelectHotGasByOwnerId")
    @ResponseBody

    public Result OwnerSelectHotGasByOwnerId(Integer ownerid){
        List<Hotgas> hotgases = hotGasService.OwnerselectHotGasByOwnerId(ownerid);
        return Result.success(hotgases);
    }
    /*---------------------------------水费---------------------------------*/
    @RequestMapping("/selectItemById")
    @ResponseBody
    public Result selectItemById(Integer id){
        Item item = itemService.selectItemById(id);
        return Result.success(item);
    }
    @RequestMapping("/selectMaxIdHydrop")
    @ResponseBody
    public Result selectMaxIdHydrop(Integer plotid){
        String i = hydropowerService.selectMaxByplotid(plotid);
        if(i==null){
            return Result.success(0);
        }else {
            int id= Integer.parseInt(i);
            Hydropower hydropower = hydropowerService.selectHydropById(id);
            return Result.success(hydropower);
        }
    }
    @ResponseBody
    @RequestMapping("/addHyPowerWater")
    public Result addHyPowerWater(Hydropower hydropower){
        LocalDateTime localDateTime = LocalDateTime.now();
        String paytime=localDateTime.toString();
        hydropower.setPaytime(paytime);
        try {
            int i = hydropowerService.addHyDropWater(hydropower);
            return Result.success("缴费成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("缴费失败");
        }
    }
    @ResponseBody
    @RequestMapping("/selectMaxe")
    public Result selectMaxIdBydrope(Integer eplotid){
        String i = hydropowerService.selectMaxByploteid(eplotid);
        if(i==null){
            return Result.success(0);
        }else {
            int id= Integer.parseInt(i);
            Hydropower hydropower = hydropowerService.selectHydropById(id);
            return Result.success(hydropower);
        }
    }
    @ResponseBody
    @RequestMapping("/addHyPowere")
    public Result addHyPowere(HydropowerVo hydropowerVo){
        LocalDateTime localDateTime = LocalDateTime.now();
        String paytime=localDateTime.toString();
        hydropowerVo.setEpaytime(paytime);
        try {
            System.out.println(hydropowerVo);
            int i = hydropowerService.addHyDropE(hydropowerVo);
            return Result.success("缴费成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("缴费失败");
        }
    }
    @ResponseBody
    @RequestMapping("/selectPayByOwnerid")
    public Result selectPayOwnerid(Integer ownerid){
        List<Pay> pays = payService.selectPayByOwnerid(ownerid);
        return Result.success(pays);
    }
    @ResponseBody
    @RequestMapping("/updatePay")
    public Result updatePay(PayVo payVo){
        try {
            LocalDateTime localDateTime = LocalDateTime.now();
            String paytime = localDateTime.toString();
            payVo.setPaytime(paytime);
            int i = payService.updatePay(payVo);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }

    }
}
